﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Xml;
using System.IO;


namespace eSYSU.Weibo
{
    /// <summary>
    /// 微博返回的XML字符串处理类
    /// </summary>
    public class WeiboXMLHandler
    {
        private XmlDocument xmlDoc = null;

        public WeiboXMLHandler()
        {
            xmlDoc = new XmlDocument();
        }

        #region Functions
        /// <summary>
        /// 加载xml文件
        /// </summary>
        /// <param name="fileName">xml 文件路径</param>
        public void Load(string fileName)
        {
            xmlDoc.Load(fileName);
        }


        /// <summary>
        /// 读取XML格式的字符串
        /// </summary>
        /// <param name="xml">XML字符串</param>
        public void LoadXml(string xml)
        {
            xmlDoc.LoadXml(xml);
        }


        /// <summary>
        /// xpath 是否存在
        /// </summary>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public bool IsExists(string xpath)
        {
            return xmlDoc.DocumentElement.SelectSingleNode(xpath) != null ? true : false;
        }


        /// <summary>
        /// xpath 是否存在
        /// </summary>
        /// <param name="xn">XML节点</param>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public bool IsExists(XmlNode xn, string xpath)
        {
            return xn.SelectSingleNode(xpath) != null ? true : false;
        }


        /// <summary>
        /// 选择指定节点
        /// </summary>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public XmlNodeList SelectNodes(string xpath)
        {
            return xmlDoc.DocumentElement.SelectNodes(xpath);
        }


        /// <summary>
        /// 选择指定节点
        /// </summary>
        /// <param name="xn">节点</param>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public XmlNodeList SelectNodes(XmlNode xn, string xpath)
        {
            return xn.SelectNodes(xpath);
        }


        /// <summary>
        /// 选择单节点
        /// </summary>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public string SelectSingleNode(string xpath)
        {
            return IsExists(xpath) ? xmlDoc.DocumentElement.SelectSingleNode(xpath).InnerText : string.Empty;
        }


        /// <summary>
        /// 选择单节点
        /// </summary>
        /// <param name="xn">节点</param>
        /// <param name="xpath">xpath 路径</param>
        /// <returns></returns>
        public string SelectSingleNode(XmlNode xn, string xpath)
        {
            return IsExists(xn, xpath) ? xn.SelectSingleNode(xpath).InnerText : string.Empty;
        }
        #endregion
    }



    /// <summary>
    /// 实体类SinaWeiboUsers 。(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public class SinaWeiboUsers
    {
        public SinaWeiboUsers()
        { }

        #region Model
        private long _id;
        private string _screen_name;
        private string _name;
        private string _url;
        private string _profile_image_url;
        private string _domain;
        private string _gender;
        private long _followers_count;
        private long _friends_count;
        private long _statuses_count;
        private long _favourites_count;
        private DateTime? _created_at;
        private bool _following;
        private bool _verified;
        private string _province;
        private string _city;
        private string _location;
        private string _description;

        /// <summary>
        /// 用户UID 
        /// </summary>
        public long id
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// 微博昵称 
        /// </summary>
        public string screen_name
        {
            set { _screen_name = value; }
            get { return _screen_name; }
        }

        /// <summary>
        /// 友好显示名称，如Bill Gates
        /// </summary>
        public string name
        {
            set { _name = value; }
            get { return _name; }
        }

        /// <summary>
        /// 用户博客地址
        /// </summary>
        public string url
        {
            set { _url = value; }
            get { return _url; }
        }

        /// <summary>
        /// 自定义图像
        /// </summary>
        public string profile_image_url
        {
            set { _profile_image_url = value; }
            get { return _profile_image_url; }
        }

        /// <summary>
        /// 用户个性化URL 
        /// </summary>
        public string domain
        {
            set { _domain = value; }
            get { return _domain; }
        }

        /// <summary>
        /// 性别,m--男，f--女,n--未知 
        /// </summary>
        public string gender
        {
            set { _gender = value; }
            get { return _gender; }
        }

        /// <summary>
        /// 粉丝数
        /// </summary>
        public long followers_count
        {
            set { _followers_count = value; }
            get { return _followers_count; }
        }

        /// <summary>
        /// 关注数
        /// </summary>
        public long friends_count
        {
            set { _friends_count = value; }
            get { return _friends_count; }
        }

        /// <summary>
        /// 微博数
        /// </summary>
        public long statuses_count
        {
            set { _statuses_count = value; }
            get { return _statuses_count; }
        }

        /// <summary>
        /// 收藏数
        /// </summary>
        public long favourites_count
        {
            set { _favourites_count = value; }
            get { return _favourites_count; }
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? created_at
        {
            set { _created_at = value; }
            get { return _created_at; }
        }

        /// <summary>
        /// 是否已关注
        /// </summary>
        public bool following
        {
            set { _following = value; }
            get { return _following; }
        }

        /// <summary>
        /// 加V标示，是否微博认证用户
        /// </summary>
        public bool verified
        {
            set { _verified = value; }
            get { return _verified; }
        }

        /// <summary>
        /// 省份编码（参考省份编码表）
        /// </summary>
        public string province
        {
            set { _province = value; }
            get { return _province; }
        }

        /// <summary>
        /// 城市编码（参考城市编码表）
        /// </summary>
        public string city
        {
            set { _city = value; }
            get { return _city; }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string location
        {
            set { _location = value; }
            get { return _location; }
        }

        /// <summary>
        /// 个人描述
        /// </summary>
        public string description
        {
            set { _description = value; }
            get { return _description; }
        }
        #endregion Model
    }




    /// <summary>
    /// 实体类SinaWeiboStatuses (自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public class SinaWeiboStatuses
    {
        public SinaWeiboStatuses()
        {
        }

        #region Model
        private long _user_id;
        private long _id;
        private DateTime? _created_at;
        private string _text;
        private string _source;
        private bool _favorited;
        private bool _truncated;
        private long _in_reply_to_status_id;
        private long _in_reply_to_user_id;
        private string _in_reply_to_screen_name;
        private string _thumbnail_pic;
        private string _bmiddle_pic;
        private string _original_pic;
        private long _retweeted_status_id;

        /// <summary>
        /// 用户ID
        /// </summary>
        public long user_id
        {
            set { _user_id = value; }
            get { return _user_id; }
        }


        /// <summary>
        /// 微博ID 
        /// </summary>
        public long id
        {
            set { _id = value; }
            get { return _id; }
        }


        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? created_at
        {
            set { _created_at = value; }
            get { return _created_at; }
        }


        /// <summary>
        /// 微博信息内容
        /// </summary>
        public string text
        {
            set { _text = value; }
            get { return _text; }
        }


        /// <summary>
        /// 微博来源
        /// </summary>
        public string source
        {
            set { _source = value; }
            get { return _source; }
        }


        /// <summary>
        /// 是否已收藏
        /// </summary>
        public bool favorited
        {
            set { _favorited = value; }
            get { return _favorited; }
        }


        /// <summary>
        /// 是否被截断
        /// </summary>
        public bool truncated
        {
            set { _truncated = value; }
            get { return _truncated; }
        }

        /// <summary>
        /// 回复ID 
        /// </summary>
        public long in_reply_to_status_id
        {
            set { _in_reply_to_status_id = value; }
            get { return _in_reply_to_status_id; }
        }

        /// <summary>
        /// 回复人UID 
        /// </summary>
        public long in_reply_to_user_id
        {
            set { _in_reply_to_user_id = value; }
            get { return _in_reply_to_user_id; }
        }


        /// <summary>
        /// 回复人昵称
        /// </summary>
        public string in_reply_to_screen_name
        {
            set { _in_reply_to_screen_name = value; }
            get { return _in_reply_to_screen_name; }
        }


        /// <summary>
        /// 缩略图
        /// </summary>
        public string thumbnail_pic
        {
            set { _thumbnail_pic = value; }
            get { return _thumbnail_pic; }
        }


        /// <summary>
        /// 中型图片
        /// </summary>
        public string bmiddle_pic
        {
            set { _bmiddle_pic = value; }
            get { return _bmiddle_pic; }
        }


        /// <summary>
        /// 原始图片
        /// </summary>
        public string original_pic
        {
            set { _original_pic = value; }
            get { return _original_pic; }
        }


        /// <summary>
        /// 转发的博文ID，内容为status，如果不是转发，为0
        /// </summary>
        public long retweeted_status_id
        {
            set { _retweeted_status_id = value; }
            get { return _retweeted_status_id; }
        }
        #endregion Model
    }




    /// <summary>
    /// 实体类SinaWeiboComments(微博评论)
    /// </summary>
    [Serializable]
    public class SinaWeiboComments
    {
        public SinaWeiboComments()
        { }

        #region Model
        private long _id;
        private string _text;
        private string _source;
        private bool _favorited;
        private bool _truncated;
        private DateTime? _created_at;
        private long _user_id;
        private long _status_id;
        private long _reply_comment_id;

        
        /// <summary>
        /// 评论ID 
        /// </summary>
        public long id
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// 评论内容
        /// </summary>
        public string text
        {
            set { _text = value; }
            get { return _text; }
        }

        /// <summary>
        /// 评论来源
        /// </summary>
        public string source
        {
            set { _source = value; }
            get { return _source; }
        }

        /// <summary>
        /// 是否收藏
        /// </summary>
        public bool favorited
        {
            set { _favorited = value; }
            get { return _favorited; }
        }

        /// <summary>
        /// 是否被截断 
        /// </summary>
        public bool truncated
        {
            set { _truncated = value; }
            get { return _truncated; }
        }

        /// <summary>
        /// 评论时间
        /// </summary>
        public DateTime? created_at
        {
            set { _created_at = value; }
            get { return _created_at; }
        }

        /// <summary>
        /// 会员ID
        /// </summary>
        public long user_id
        {
            set { _user_id = value; }
            get { return _user_id; }
        }

        /// <summary>
        /// 微博ID
        /// </summary>
        public long status_id
        {
            set { _status_id = value; }
            get { return _status_id; }
        }

        /// <summary>
        /// 评论来源
        /// </summary>
        public long reply_comment_id
        {
            set { _reply_comment_id = value; }
            get { return _reply_comment_id; }
        }

        #endregion Model
    }

}